package com.cloudptt.api.product.config.call;

import android.content.SharedPreferences;

import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;

public class AnswerModeConfig {
    public static final String Key_AutoAnswerVoice = "answer_mode_voice";
    public static final String Key_AutoAnswerVideo = "answer_mode_video";
    public static final String Key_AutoAnswerVoiceGroup = "answer_mode_voice_group";
    public static final String Key_AutoAnswerVideoGroup = "answer_mode_video_group";

    private SharedPreferences sp = null;

    private boolean autoAnswerVoice = false;
    private boolean autoAnswerVideo = false;
    private boolean autoAnswerVoiceGroup = false;
    private boolean autoAnswerVideoGroup = false;

    public AnswerModeConfig(SharedPreferences sp)
    {
        this.sp = sp;
        this.autoAnswerVoice = sp.getBoolean(Key_AutoAnswerVoice,false);
        this.autoAnswerVideo = sp.getBoolean(Key_AutoAnswerVideo,false);
        this.autoAnswerVoiceGroup = sp.getBoolean(Key_AutoAnswerVoiceGroup,true);
        this.autoAnswerVideoGroup = sp.getBoolean(Key_AutoAnswerVideoGroup,true);
    }

    public void setAutoAnswerVoice(boolean autoAnswerVoice) {
        this.autoAnswerVoice = autoAnswerVoice;
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(Key_AutoAnswerVoice, autoAnswerVoice);

        editor.commit();
    }

    public void setAutoAnswerVideo(boolean autoAnswerVideo) {
        this.autoAnswerVideo = autoAnswerVideo;
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(Key_AutoAnswerVideo, autoAnswerVideo);

        editor.commit();
    }

    public boolean isAutoAnswer(int callType,boolean hasVideo) {
        Log.debug(this.getClass(), "isAutoAnswer callType = " + callType + " hasVideo = " + hasVideo);
        if(CallAttr.isGroupCall(callType))
        {
            if(hasVideo)
            {
                return autoAnswerVideoGroup;
            }
            else
            {
                return autoAnswerVoiceGroup;
            }
        }
        else if(CallAttr.isPrivateCall(callType))
        {
            if(hasVideo)
            {
                return autoAnswerVideo;
            }
            else
            {
                return autoAnswerVoice;
            }
        }
        else if(CallAttr.isAmbient(callType))
        {
            return true;
        }
        return false;
    }

    public boolean isAutoAnswerVoice() {
        return autoAnswerVoice;
    }

    public boolean isAutoAnswerVideo() {
        return autoAnswerVideo;
    }

    public boolean isAutoAnswerVoiceGroup() {
        return autoAnswerVoiceGroup;
    }

    public void setAutoAnswerVoiceGroup(boolean autoAnswerVoiceGroup) {
        this.autoAnswerVoiceGroup = autoAnswerVoiceGroup;
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(Key_AutoAnswerVoiceGroup, autoAnswerVoiceGroup);

        editor.commit();
    }

    public boolean isAutoAnswerVideoGroup() {
        return autoAnswerVideoGroup;
    }

    public void setAutoAnswerVideoGroup(boolean autoAnswerVideoGroup) {
        this.autoAnswerVideoGroup = autoAnswerVideoGroup;
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(Key_AutoAnswerVideoGroup, autoAnswerVideoGroup);

        editor.commit();
    }
    public String toString()
    {
        StringBuffer strBuf = new StringBuffer("" + this.getClass() + ":");
        strBuf.append("answerModeVoice = ").append(autoAnswerVoice).append("  ");
        strBuf.append("answerModeVideo = ").append(autoAnswerVideo).append("  ");
        strBuf.append("autoAnswerVoiceGroup = ").append(autoAnswerVoiceGroup).append("  ");
        strBuf.append("autoAnswerVideoGroup = ").append(autoAnswerVideoGroup).append("  ");

        return strBuf.toString();
    }
}
